/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.support.planning;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import com.qihoo.gamecenter.sdk.common.h.c;
import com.qihoo.gamecenter.sdk.common.i.s;
import com.qihoo.gamecenter.sdk.support.planning.view.PlanningView;
import com.qihoo.gamecenter.sdk.support.planning.view.PlanningView.a;
import com.qihoo.gamecenter.sdk.support.systemmessage.b;
import com.qihoo.stat.QHStatDo;
import java.lang.ref.WeakReference;
import org.json.JSONObject;

public class a {
    private Context a;
    private PlanningView b;
    private WindowManager c;
    private WindowManager.LayoutParams d;
    private static a e = null;
    private String f;
    private String g;
    private boolean h = false;
    private a i;
    private BroadcastReceiver j;

    private a(Context paramContext) {
        this.a = paramContext;
        this.i = new a(this);
        a();
    }

    public static a a(Context paramContext) {
        if (e == null)
            e = new a(paramContext);
        return e;
    }

    private void a() {
        this.c = ((WindowManager) this.a.getSystemService("window"));
        this.d = new WindowManager.LayoutParams();
        this.d.type = 2002;
        this.d.gravity = 17;
        this.d.format = -2;
        this.b = new PlanningView(this.a);
        this.b.setOnPlanningClickListener(new PlanningView.a() {
            public void a() {
                a.b(a.this);
                QHStatDo.event("360sdk_huodong_ok_btn_style2", null);
                a.c(a.this).sendEmptyMessageDelayed(100, 100L);
            }

            public void b() {
                a.b(a.this);
                com.qihoo.gamecenter.sdk.support.bbs.a.a(a.a(a.this), new Intent(), a.d(a.this), "huodong");
                QHStatDo.event("360sdk_huodong_info_btn_style2", null);
            }
        });
    }

    private void b() {
        com.qihoo.gamecenter.sdk.support.g.a.a("PlanningManager", new Object[] { " --> hidePlanningView()" });
        if ((this.b != null) && (this.b.getParent() != null)) {
            this.c.removeView(this.b);
            this.h = false;
        }
        h();
    }

    private void a(String paramString1, String paramString2, String paramString3) {
        g();
        if (!(f()))
            return;
        if ((!(TextUtils.isEmpty(paramString2))) && (!(TextUtils.isEmpty(paramString3))) && (s.n(this.a)))
            this.b.a(paramString2, paramString3, paramString1);
        else
            this.b.a(new View.OnClickListener() {
                public void onClick(View paramView) {
                    a.e(a.this).a();
                    a.this.a(a.f(a.this));
                    a.a(a.this, true);
                }
            });
        if (this.h)
            return;
        this.c.addView(this.b, this.d);
        this.h = true;
    }

    public void a(String paramString) {
        this.f = paramString;
        Intent localIntent = new Intent();
        if (!(TextUtils.isEmpty(paramString)))
            localIntent.putExtra("qid", paramString);
        new com.qihoo.gamecenter.sdk.support.planning.a.a(this.a, localIntent).a(new c() {
            public void a(String paramString, Context paramContext) {
                super.a(paramString, paramContext);
            }

            public void a(int paramInt, String paramString, JSONObject paramJSONObject) {
                com.qihoo.gamecenter.sdk.support.g.a.a("PlanningManager", new Object[] {
                        " --> PlanningTask.callBack(Context ctx) -- > localErrCode = ", Integer.valueOf(paramInt),
                        "> localErrMsg =", paramString });
                com.qihoo.gamecenter.sdk.support.g.a.a("PlanningManager", new Object[] {
                        " --> PlanningTask.callBack(Context ctx) -- > remoteContent = ", paramJSONObject });
                JSONObject localJSONObject = paramJSONObject;
                Boolean localBoolean = Boolean.valueOf(true);
                if ((paramInt == 0) && (localJSONObject != null)) {
                    localJSONObject = localJSONObject.optJSONObject("data");
                    if (localJSONObject != null) {
                        localBoolean = Boolean.valueOf(localJSONObject.optBoolean("has_hd"));
                        if (localBoolean.booleanValue()) {
                            localJSONObject = localJSONObject.optJSONObject("hd");
                            String str1 = localJSONObject.optString("title");
                            String str2 = localJSONObject.optString("description");
                            a.a(a.this, localJSONObject.optString("direct_url"));
                            int i = localJSONObject.optInt("display_perday");
                            a.a(a.this, i);
                            a.a(a.this, a.d(a.this), str1, str2);
                        }
                        else {
                            a.c(a.this).sendEmptyMessageDelayed(100, 100L);
                        }
                        return;
                    }
                    a.c(a.this).sendEmptyMessageDelayed(100, 100L);
                }
                else {
                    a.a(a.this, "", "", "");
                }
            }
        }, new String[0]);
    }

    private void c() {
        SharedPreferences localSharedPreferences = this.a.getSharedPreferences("huodong_dispay_perday", 0);
        localSharedPreferences.edit().putString("huodong_display_qid", this.f).commit();
        localSharedPreferences.edit().putString("huodong_display_appkey", s.r(this.a)).commit();
        localSharedPreferences.edit().putLong("huodong_display_time", System.currentTimeMillis()).commit();
    }

    private void a(int paramInt) {
        SharedPreferences localSharedPreferences = this.a.getSharedPreferences("huodong_dispay_perday", 0);
        localSharedPreferences.edit().putInt("huodong_dispay_perday", paramInt).commit();
    }

    private int d() {
        SharedPreferences localSharedPreferences = this.a.getSharedPreferences("huodong_dispay_perday", 0);
        return localSharedPreferences.getInt("huodong_dispay_perday", 0);
    }

    private void b(int paramInt) {
        SharedPreferences localSharedPreferences = this.a.getSharedPreferences("huodong_dispay_perday", 0);
        localSharedPreferences.edit().putInt("huodong_dispay_count", paramInt).commit();
    }

    private int e() {
        SharedPreferences localSharedPreferences = this.a.getSharedPreferences("huodong_dispay_perday", 0);
        return localSharedPreferences.getInt("huodong_dispay_count", 0);
    }

    private boolean f() {
        com.qihoo.gamecenter.sdk.support.g.a.a("PlanningManager", new Object[] { "isHuodongDisplay" });
        SharedPreferences localSharedPreferences = this.a.getSharedPreferences("huodong_dispay_perday", 0);
        String str1 = localSharedPreferences.getString("huodong_display_qid", "");
        String str2 = localSharedPreferences.getString("huodong_display_appkey", "");
        Long localLong1 = Long.valueOf(localSharedPreferences.getLong("huodong_display_time", 0L));
        int k = localSharedPreferences.getInt("huodong_dispay_count", 0);
        int l = d();
        com.qihoo.gamecenter.sdk.support.g.a.a("PlanningManager", new Object[] { "sp_qid+sp_appkey = " + str1 + " "
                + str2 });
        com.qihoo.gamecenter.sdk.support.g.a.a("PlanningManager", new Object[] { "sp_count = " + k });
        Long localLong2 = Long.valueOf(System.currentTimeMillis());
        if ((TextUtils.isEmpty(str1)) || (TextUtils.isEmpty(str2))) {
            c();
            b(1);
            return true;
        }
        Long localLong3 = Long.valueOf((localLong2.longValue() - localLong1.longValue()) / 1000L);
        int i1 = 86400;
        com.qihoo.gamecenter.sdk.support.g.a.a("PlanningManager", new Object[] { "farTime = " + localLong3 });
        if ((str1.equals(this.f)) && (str2.equals(s.r(this.a))) && (localLong3.longValue() < i1) && (k < l)) {
            b(e() + 1);
            return true;
        }
        if ((!(str1.equals(this.f))) || (!(str2.equals(s.r(this.a))))) {
            c();
            b(1);
            return true;
        }
        if (localLong3.longValue() >= i1) {
            c();
            b(1);
            return true;
        }
        return false;
    }

    private void g() {
        if (this.j != null)
            return;
        this.j = new BroadcastReceiver() {
            public void onReceive(Context paramContext, Intent paramIntent) {
                com.qihoo.gamecenter.sdk.support.g.a.a("PlanningManager", new Object[] { "registerIntentReceivers = "
                        + paramIntent.getAction() });
                if (a.e(a.this) == null)
                    return;
                if ("action_app_on_top".equals(paramIntent.getAction())) {
                    if (a.e(a.this).getVisibility() != 8)
                        return;
                    a.e(a.this).setVisibility(0);
                }
                else if ("action_app_on_background".equals(paramIntent.getAction())) {
                    if (a.e(a.this).getVisibility() != 0)
                        return;
                    a.e(a.this).setVisibility(8);
                }
                else {
                    if (!("action_matrix_destroyed".equals(paramIntent.getAction())))
                        return;
                    a.g(a.this);
                }
            }
        };
        IntentFilter localIntentFilter = new IntentFilter();
        localIntentFilter.addAction("action_app_on_top");
        localIntentFilter.addAction("action_app_on_background");
        localIntentFilter.addAction("action_matrix_destroyed");
        this.a.registerReceiver(this.j, localIntentFilter);
    }

    private void h() {
        try {
            if (this.j != null) {
                this.a.unregisterReceiver(this.j);
                this.j = null;
            }
        } catch (Exception localException) {
            com.qihoo.gamecenter.sdk.support.g.a.c("PlanningManager", new Object[] { localException.toString() });
        }
    }

    private class a extends Handler {
        private WeakReference b;

        public a(a parama) {
            this.b = new WeakReference(parama);
        }

        public void handleMessage(Message paramMessage) {
            a locala = (a) this.b.get();
            if (locala == null)
                return;
            switch (paramMessage.what) {
            case 100:
                b.a(a.a(a.this)).a();
            }
        }
    }
}